package cn.com.zetatech.loader.common.db;

import org.apache.commons.lang3.StringUtils;

import cn.com.zetatech.loader.api.exception.LoaderException;
import cn.com.zetatech.loader.common.enums.DbTypeEnums;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class StarrocksConfigInfo {
	
    private String url;
    private String username;
    private String password;
    private String ip;
    private int port;
    private String dbScheme;
	
    public StarrocksConfigInfo(MybatisDBConfig mybatisDBConfig) {
    	
    	if(mybatisDBConfig == null) {
    		throw new LoaderException("the datasource is invalid dbConfig!");
    	}
    	String dbType = mybatisDBConfig.getDbType();
        if(StringUtils.isEmpty(dbType) || !dbType.equalsIgnoreCase(DbTypeEnums.STARROCKS.name())) {
        	throw new LoaderException("the datasource is not starrocks!");
        }
    	
    	String url = mybatisDBConfig.getUrl();
		String username = mybatisDBConfig.getUsername();
		String password = mybatisDBConfig.getPassword();
		String urlStr = url.replaceAll("jdbc:mysql://", "");
        String ip = urlStr.split(":")[0];
        if(mybatisDBConfig.getStarrocksHttpStreamPort() != 0) {
        	this.port = mybatisDBConfig.getStarrocksHttpStreamPort();
        } else {
			 String port = urlStr.split(":")[1].split("/")[0];
			 this.port = Integer.valueOf(port);
		}
        String dbScheme = urlStr.split(":")[1].split("/")[1].split("\\?")[0];
    	
        this.url = url;
		this.username = username;
		this.password = password;
		this.ip = ip;
		this.dbScheme = dbScheme;
	}

	public StarrocksConfigInfo(String url, String username, String password, String ip, int port,
			String dbScheme) {
		super();
		this.url = url;
		this.username = username;
		this.password = password;
		this.ip = ip;
		this.port = port;
		this.dbScheme = dbScheme;
	}
}
